package com.exercise.animations.spring_animation

import android.view.LayoutInflater
import androidx.dynamicanimation.animation.DynamicAnimation
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
import com.exercise.animations.databinding.ActivitySpringAnimationBinding
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel

class SpringAnimationActivity : BaseActivity<BaseViewModel, ActivitySpringAnimationBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivitySpringAnimationBinding
        get() = ActivitySpringAnimationBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun initData() {
    }

    override fun initView() {
        val sprintAnim = binding.imageView.also { img ->
            SpringAnimation(img, DynamicAnimation.Y, 0f).apply {
                spring.dampingRatio = SpringForce.DAMPING_RATIO_LOW_BOUNCY
                spring.stiffness = SpringForce.STIFFNESS_LOW
            }.start()
        }

        val (anim1X, anim1Y) = binding.view1.let { view1 ->
            SpringAnimation(view1, DynamicAnimation.TRANSLATION_X) to
                    SpringAnimation(view1, DynamicAnimation.TRANSLATION_Y)
        }
        val (anim2X, anim2Y) = binding.view2.let { view2 ->
            SpringAnimation(view2, DynamicAnimation.TRANSLATION_X) to
                    SpringAnimation(view2, DynamicAnimation.TRANSLATION_Y)
        }
        anim1X.addUpdateListener { _, value, _ ->
            anim2X.animateToFinalPosition(value)
        }
        anim1Y.addUpdateListener { _, value, _ ->
            anim2Y.animateToFinalPosition(value)
        }
        anim1Y.animateToFinalPosition(0f)
        anim1X.animateToFinalPosition(0f)
    }
}